package com.tvunetworks.center.device.config.leader;

import com.tvunetworks.leader.election.starter.LeaderElection;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: DataSourceBuilder
 * @Description: 创建 DruidDataSource连接
 * @author lebron
 */
@Configuration
public class LeaderElectionConfig {

    @Value("${leader.election.task.deviceChange}")
    private String deviceChange;
    @Value("${leader.election.task.initDeviceGeo}")
    private String initDeviceGeo;

    /**
     *
     * @param redissonClient redissonClient
     * @return LeaderElection
     */
    @Bean(name = "deviceChangeElection")
    public LeaderElection deviceChangeElection(RedissonClient redissonClient) {
        LeaderElection singleLeaderElection = new LeaderElection();
        singleLeaderElection.setRedissonClient(redissonClient);
        singleLeaderElection.tryHold(deviceChange);
        return singleLeaderElection;
    }

    /**
     *
     * @param redissonClient redissonClient
     * @return LeaderElection
     */
    @Bean(name = "initDeviceGeoElection")
    public LeaderElection initDeviceGeoElection(RedissonClient redissonClient) {
        LeaderElection singleLeaderElection = new LeaderElection();
        singleLeaderElection.setRedissonClient(redissonClient);
        singleLeaderElection.tryHold(initDeviceGeo);
        return singleLeaderElection;
    }

}
